package com.moli.iov.admin.web.controller;

import com.moli.iov.admin.constants.Constants;
import com.moli.iov.admin.model.vo.req.QueryVehicleTboxListParamVo;
import com.moli.iov.admin.model.vo.req.VehicleTboxReqVo;
import com.moli.iov.admin.model.vo.req.VehicleTboxUpdateReqVo;
import com.moli.iov.admin.service.VehicleTboxService;
import com.moli.iov.base.enums.AuthorizedEnum;
import com.moli.iov.base.model.dto.req.QueryVehicleTboxListParamDto;
import com.moli.iov.base.model.dto.req.VehicleTboxReqDto;
import com.moli.iov.base.model.dto.req.VehicleTboxUpdateReqDto;
import com.moli.iov.base.model.dto.res.VehicleTboxResDto;
import com.moli.iov.model.dto.req.PageExtend;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.util.CommonUtils;
import com.moli.iov.util.ExcelUtil;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 车辆监控> 车载终端
 *
 * @author whl
 */
@Slf4j
@RequestMapping("/monitor")
@RestController
public class VehicleTboxController extends BaseController {

  @Autowired
  private VehicleTboxService tboxService;

  /**
   * 查询
   */
  @PostMapping("/vehicleTbox/list")
  public RestResponse listVehicleTbox(
      @RequestBody QueryVehicleTboxListParamVo vo) {
    assembleUserInfo(vo);
    QueryVehicleTboxListParamDto dto = new QueryVehicleTboxListParamDto();
    CommonUtils.copyProperties(vo, dto);
    return tboxService
        .listVehicleTbox(dto);
  }

  /**
   *
   * 车辆监控> 车载终端  更改sim卡
   */
  @PostMapping("/vehicleTbox/update")
  public RestResponse updateVehicleTbox(
      @Validated @RequestBody VehicleTboxUpdateReqVo vo) {
    assembleUserInfo(vo);
    VehicleTboxUpdateReqDto dto = new VehicleTboxUpdateReqDto();
    CommonUtils.copyProperties(vo, dto);
    return tboxService
        .updateVehicleTbox(dto);
  }


  /**
   * 查询
   */
  @PostMapping("/vehicleTbox/export")
  public RestResponse listVehicleTboxExport(
      @RequestBody QueryVehicleTboxListParamVo vo,
      HttpServletRequest request,
      HttpServletResponse response) {
    assembleUserInfo(vo);
    QueryVehicleTboxListParamDto dto = new QueryVehicleTboxListParamDto();
    CommonUtils.copyProperties(vo, dto);
    dto.setPageNum((long) 1);
    dto.setPageSize(Constants.EXPORT_PAGE_SIZE);
    RestResponse<PageExtend<VehicleTboxResDto>> restResponse = tboxService
        .listVehicleTbox(dto);
    if (restResponse==null||!restResponse.isFlag()) {
      return RestResponse.failure(restResponse.getMessage());
    }
    if (restResponse.getData() == null || restResponse.getData().getRecords().isEmpty()) {
      return RestResponse.failure("数据为空");
    }
    String[] propNames = {"deviceId", "terminalModelName", "iccid", "companyName",
        "simNo", "vin", "onLineStr", "registerTime"};
    String[] headStrs = {"T-BoxID", "T-Box型号", "ICCID", "所属公司", "SIM卡号", "车架号", "在线状态", "入网时间"};
    String title = "车机型号";
    List data = restResponse.getData().getRecords();
    try {
          ExcelUtil.exportDataExcel(request, response, data, propNames, headStrs, title);
      return RestResponse.success();
    } catch (Exception e) {
      e.printStackTrace();
      return RestResponse.failure("导出异常");
    }

  }
  /**
   *
   * 车辆监控> 车载终端  详情
   */
  @PostMapping("/vehicleTbox/details")
  public RestResponse detailsVehicleTbox(
      @Validated(QueryVehicleTboxListParamVo.Details.class) @RequestBody QueryVehicleTboxListParamVo vo) {
    assembleUserInfo(vo);
    QueryVehicleTboxListParamDto dto = new QueryVehicleTboxListParamDto();
    CommonUtils.copyProperties(vo, dto);
    try {
      return tboxService
          .getVehicleTboxDetails(dto);
    } catch (Exception e) {
      e.printStackTrace();
      return RestResponse.failure(e.getMessage());
    }
  }

  /**
   * 车辆监控> 车载终端 -详情
   *
   * @param vo the vo
   * @return the rest response
   * @version 3.01
   * @author wanghualin
   */
  @PostMapping("/vehicleTbox/deviceModel/update")
  public RestResponse updateVehicleTbox(
          @Validated(VehicleTboxReqVo.Update.class) @RequestBody VehicleTboxReqVo vo) {
    assembleUserInfo(vo);
    VehicleTboxReqDto dto = new VehicleTboxReqDto();
    CommonUtils.copyProperties(vo, dto);
    try {
      return tboxService.
              updateVehicleTboxDeviceModel(dto);
    } catch (Exception e) {
      e.printStackTrace();
      return RestResponse.failure("失败");
    }
  }
  /**
   * 车辆监控> 车载终端 -添加
   *  //TODO
   * @param vo the vo
   * @return the rest response
   * @version 3.01
   * @author wanghualin
   */
  @PostMapping("/vehicleTbox/save")
  public RestResponse saveVehicleTbox(
          @Validated(VehicleTboxReqVo.Update.class) @RequestBody VehicleTboxReqVo vo) {
    assembleUserInfo(vo);
    VehicleTboxReqDto dto = new VehicleTboxReqDto();
    CommonUtils.copyProperties(vo, dto);
    //设置为已授权
    dto.setAuthorizedStatus(AuthorizedEnum.AUTHORIZED.getCode());
    try {
      return tboxService.
             saveVehicleTboxDeviceModel(dto);
    } catch (Exception e) {
      e.printStackTrace();
      return RestResponse.failure("失败");
    }
  }
}
